/**
 * @author Isabella Nake (isabella.nake@usi.ch)
 * @author Nicolas Lusa (lusan@usi.ch)
 */
package ch.usi.inf.pf2.time;

import java.util.ArrayList;

import ch.usi.inf.pf2.circuit.Value;

public class Schedule {
	private static ArrayList<Event> queue = new ArrayList<Event>();
	private static int step = 0;
	
	public Schedule(){
		
	}
	
	public static void addEvent(final Event ev){
		queue.add(ev);
	}
	
	public static void printEvents(){
		for(Event event : queue){
			event.printEvent();
		}
	}
	
	public static int getStep() {
		return step;
	}
	
	public static void resetStep(){
		step = 0;
	}
	
	public static void run() {
		while(!queue.isEmpty()) {
			for(int i = 0; i < queue.size(); i++) {
				if(queue.get(i).getTime() == step) {
					queue.get(i).run();
					queue.get(i).printEvent();
					queue.remove(i);
					i--;
				}
			}
			step++;
		}
	}
	
	public static void stepRun() {
		System.out.println("queue - size: " + queue.size());
		System.out.println("step: " + step);
		for(int i = 0; i < queue.size(); i++) {
			if(queue.get(i).getTime() == step) {
				queue.get(i).run();
				queue.get(i).printEvent();
				queue.remove(i);
				i--;
			}
		}
		step++;
		if(queue.isEmpty()) {
			resetStep();
		}
	}
	
	public static void clearQueue() {
		queue = new ArrayList<Event>();
	}
}
